/**
 * Project:   dtu.fmes.googleloon
 * File:      SimulationState.java
 * License: 
 *            This file is licensed under GNU General Public License version 3
 *            http://www.gnu.org/licenses/gpl-3.0.txt
 *
 * Copyright:	Bartosz Cichecki [ cichecki.bartosz@gmail.com ]
 * 				Nicolai Sonne [ NicolaiSonne@hotmail.com.com ]
 * Date:      Dec 11, 2013
 */

package dtu.fmes.loon.model;

import java.util.ArrayList;
import java.util.List;

/**
 * Represents current simulation state, which consists of timestamp and all balloons.
 * 
 * @author Bartosz Cichecki, Nicolai Sonne
 * 
 */
public class SimulationState implements Comparable<SimulationState> {

	private List<Balloon> balloons = null;

	private ConstantBalloonValues constantBalloonValues = null;

	private Environment environment = null;

	private Simulation simulation = null;

	public SimulationState() {
		super();
	}

	public SimulationState(List<Balloon> balloons, ConstantBalloonValues constantBalloonValues, Environment environment,
	        Simulation simulation) {
		super();
		this.balloons = balloons;
		this.constantBalloonValues = constantBalloonValues;
		this.environment = environment;
		this.simulation = simulation;
	}

	@Override
	public int compareTo(SimulationState o) {
		if (getSimulation().getCurrentTime() == o.getSimulation().getCurrentTime()) {
			return 0;
		}
		if (getSimulation().getCurrentTime() < o.getSimulation().getCurrentTime()) {
			return -1;
		} else {
			return 1;
		}
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		SimulationState other = (SimulationState) obj;
		if (balloons == null) {
			if (other.balloons != null) {
				return false;
			}
		} else if (!balloons.equals(other.balloons)) {
			return false;
		}
		if (constantBalloonValues == null) {
			if (other.constantBalloonValues != null) {
				return false;
			}
		} else if (!constantBalloonValues.equals(other.constantBalloonValues)) {
			return false;
		}
		if (environment == null) {
			if (other.environment != null) {
				return false;
			}
		} else if (!environment.equals(other.environment)) {
			return false;
		}
		if (simulation == null) {
			if (other.simulation != null) {
				return false;
			}
		} else if (!simulation.equals(other.simulation)) {
			return false;
		}
		return true;
	}

	public List<Balloon> getBalloons() {
		return balloons;
	}

	public ConstantBalloonValues getConstantBalloonValues() {
		return constantBalloonValues;
	}

	public Environment getEnvironment() {
		return environment;
	}

	/**
	 * Creates export clone, which only contains values relevant for the output
	 * 
	 * @return
	 */
	public SimulationState getExportClone() {
		SimulationState exportClone = new SimulationState();

		List<Balloon> exportBalloons = new ArrayList<Balloon>();
		for (Balloon b : getBalloons()) {
			exportBalloons.add(b.getExportClone(simulation));
		}
		exportClone.setBalloons(exportBalloons);
		exportClone.setSimulation(getSimulation().getExportClone());

		return exportClone;
	}

	public Simulation getSimulation() {
		return simulation;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (balloons == null ? 0 : balloons.hashCode());
		result = prime * result + (constantBalloonValues == null ? 0 : constantBalloonValues.hashCode());
		result = prime * result + (environment == null ? 0 : environment.hashCode());
		result = prime * result + (simulation == null ? 0 : simulation.hashCode());
		return result;
	}

	public void setBalloons(List<Balloon> balloons) {
		this.balloons = balloons;
	}

	public void setConstantBalloonValues(ConstantBalloonValues constantBalloonValues) {
		this.constantBalloonValues = constantBalloonValues;
	}

	public void setEnvironment(Environment environment) {
		this.environment = environment;
	}

	public void setSimulation(Simulation simulation) {
		this.simulation = simulation;
	}

	@Override
	public String toString() {
		return "SimulationState [balloons=" + balloons + ", constantBalloonValues=" + constantBalloonValues + ", environment="
		        + environment + ", simulation=" + simulation + "]";
	}
}
